<?php
/**
 * author james
 * date	2013-03-30
 * desc	some validator class,for example,general validator class,injecting class.
 *
 */

class validator
{
	private function __construct(){}

	private function __clone(){}

	public static function &get_instance()
	{
		if(!(self::$mobj_instance instanceof self)) self::$mobj_instance=new self();
		return self::$mobj_instance;
	}

	public function is_valid_nickname($lstr_nickname)
	{
		if(!is_string($lstr_nickname)) return false;
		if(preg_match_all('/^[a-zA-Z0-9\x{4e00}-\x{9fa5}_]{4,32}$/u',$lstr_nickname,$match)==0) return false;
		return true;
	}

	public function is_valid_email($lstr_email)
	{
		if(!is_string($lstr_email)) return false;
		if(preg_match_all('/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/',$lstr_email,$match)==0) return false;
		return true;
	}

	public function is_valid_idcard($lstr_idcard)
	{
		if(!is_string($lstr_idcard)) return false;
		if(preg_match_all('/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/',$lstr_idcard,$match)==0) return false;
		return true;
	}

	public function is_valid_phone($lstr_phone)
	{
		if(!is_string($lstr_phone)) return false;
		if(preg_match_all('/^1[0-9]{10}$/',$lstr_phone,$match)==0) return false;
		return true;
	}

	public function is_valid_password($lstr_password)
	{
		if(!is_string($lstr_password)) return false;
		if(preg_match_all('/^[a-zA-Z0-9_]{6,16}$/',$lstr_password,$match)==0) return false;
		return true;
	}

	public function is_valid_num_char_string($lstr_str)
	{
		if(!is_string($lstr_str)) return false;
		if(preg_match('/[^\w]+/',$lstr_str,$match)>0) return false;
		return true;
	}

	public function is_valid_sql_string($lstr_str,$li_len)
	{
		if(!is_string($lstr_str)) return false;
		$li_len1=mb_strlen($lstr_str,'utf-8');
		if($li_len1>$li_len || $li_len1<=0) return false;
		if(preg_match('/(\'|(\%27)|\;|(\%3b)|\=|(\%3d)|\(|(\%28)|\)|(\%29)|(\/\*)|(\%2f\%2a)|(\*\/)|(\%2a\%2f)|\+|(\%2b)|\<|(\%3c)|\>|(\%3e)|(\-\-)|\[|\%5b|\]|\%5d)/', $lstr_str)>0) return false;
		return true;
	}

	public function is_valid_company($lstr_compnay)
	{
		if(!is_string($lstr_compnay)) return false;
		if(!$this->is_valid_sql_string($lstr_compnay,32)) return false;
		return true;
	}

	public function is_valid_industry3($lstr_industry3)
	{
		if(!is_string($lstr_industry3)) return false;
		if(!$this->is_valid_sql_string($lstr_industry3,10)) return false;
		return true;
	}

	public function is_valid_contactor($lstr_contactor)
	{
		if(!is_string($lstr_contactor)) return false;
		if(!$this->is_valid_sql_string($lstr_contactor,16)) return false;
		return true;
	}

	public function is_valid_tel($lstr_tel)
	{
		if(!is_string($lstr_tel)) return false;
		if(strlen($lstr_tel)>16) return false;
		if(preg_match('/^((\d{3}-)?\d{8}|(\d{4}-)?\d{7}|(\d{4}-)?\d{8})$/', $lstr_tel)>0) return true;
		return false;
	}

	public function is_valid_register_number($lstr_register_number)
	{
		if(!is_string($lstr_register_number)) return false;
		if(preg_match_all('/^[0-9]{15}$/',$lstr_register_number,$match)==0) return false;
		return true;
	}

	public function is_valid_url($url)
	{
		if(!is_string($url)) return false;
		if(strlen($url)>255) return false;
		if(preg_match('_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS',$url,$match)==0) return false;
		return true;
	}

	public function is_valid_id($id)
	{
		if(!is_string($id)) return false;
		if(strlen($id)>32) return false;
		if(preg_match_all('/^[a-zA-Z_]\w{0,31}$/',$id,$match)==0) return false;
		return true;
	}
	private static $mobj_instance=null;
}
?>